package com.zy.qudadid.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rey.material.widget.Button;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.bean.Users;
import com.zy.lcpassenger.R;
import com.zy.qudadid.model.ReportTitle;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.presenter.OwnYijianPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.adapter.ReportTitleAdapter;
import com.zy.qudadid.ui.view.OwnYijianView;
import com.zy.qudadid.ui.widget.GlideCircleTransform;
import com.zy.qudadid.ui.widget.ListViewDialog;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.ToolsUtil;
import com.zy.util.JsonUtils;
import com.zy.util.UserUtil;
import com.zy.util.Util;
//import com.zhy.http.okhttp.OkHttpUtils;
//import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by ninos on 2016/7/29.
 */
public class OwnYijianActivity extends ToolBarActivity<OwnYijianPresenter> implements OwnYijianView {

    @Bind(R.id.cz_image)
    ImageView cz_image;
    @Bind(R.id.cz_name)
    TextView cz_name;
    @Bind(R.id.txtTitle)
    TextView txtTitle;
    @Bind(R.id.cz_carnumber)
    TextView cz_carnumber;
    @Bind(R.id.cz_call)
    ImageView cz_call;
    @Bind(R.id.oy_text)
    EditText oyText;
    @Bind(R.id.oy_done)
    Button oyDone;
    String did = "";
    String avatar = "";
    String name="",phone="",orderid="";

    @Override
    protected CharSequence provideTitle() {
        return "投诉";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_own_yijian;
    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        did = getIntent().getBundleExtra("data").getString("did");
        name = getIntent().getBundleExtra("data").getString("name");
        phone = getIntent().getBundleExtra("data").getString("mobile");
        orderid = getIntent().getBundleExtra("data").getString("orderid");
        avatar = getIntent().getBundleExtra("data").getString("avatar");

        cz_name.setText(name);
        cz_carnumber.setText(phone);
        Glide.with(getContext())
                .load(Const.BASE_IMG+avatar)
                .placeholder(R.drawable.adm)
                .into(cz_image);

        cz_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent();
                it.setAction(Intent.ACTION_CALL);
                it.setData(Uri.parse("tel:" + phone));
                startActivity(it);
            }
        });
//        Map map = new HashMap();
//        map.put("user_id", did);
//        String json = StringUtil.toJson(map);
//        try {
//            String data = AESCrypt.getInstance().encrypt(json);
//            String d = StringUtil.replaceBlank(data);
//            presenter.getpeopleinfo(d);
//        } catch (Exception ex) {
//        }
    }

    // 举报标题
    ArrayList<ReportTitle> listReport = new ArrayList<ReportTitle>();

    @Override
    public void initListeners() {
        txtTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String,String> map = new HashMap<String, String>();
                map.put("type","2");
                String json = JsonUtils.toJson(map);
                com.zy.base.AESCrypt aesCrypt = null;
                try {
                    aesCrypt = new com.zy.base.AESCrypt();
                    String d = Util.replaceBlank(aesCrypt.encrypt(json));
                    OkHttpUtils.post()
                            .url(Const.BASE_URL+"api.php/Order/dictionaries_title")
                            .addParams("args",d)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int i) {

                                }

                                @Override
                                public void onResponse(String response,int i) {
                                    ToolsUtil.print("----","获取订单标题返回值："+response);
                                    JSONObject json;
                                    try {
                                        json = new JSONObject(response);
                                        int code = json.getInt("code");
                                        if(code == 200){
                                            JSONArray datas = json.getJSONArray("datas");
                                            listReport.clear();
                                            listReport = new Gson().fromJson(datas.toString(),new TypeToken<ArrayList<ReportTitle>>(){}.getType());

                                            final ListViewDialog dialog2 = new ListViewDialog(getContext());
                                            ReportTitleAdapter adapter = new ReportTitleAdapter(getContext(),listReport);
                                            dialog2.listview.setAdapter(adapter);

                                            dialog2.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                    txtTitle.setText(listReport.get(position).getTitle());
                                                    dialog2.dismiss();
                                                }
                                            });

                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });
    }

    @Override
    public OwnYijianPresenter createPresenter() {
        return new OwnYijianPresenter();
    }

    @OnClick(R.id.oy_done)
    public void onClick() {
        if(oyText.getText().toString().trim().equals("")){
            toast("请认真填写您对司机的意见，不要提交空内容");
            return;
        }



        if(txtTitle.getText().toString().trim().equals("")){
            toast("请选择投诉标题");
            return;
        }
        Map map = new HashMap();
        map.put("user_id", new UserUtil(getContext()).getUserId());
        map.put("complain_id", did);
        map.put("title", txtTitle.getText().toString());
        map.put("order_id", orderid);
        map.put("type", "1");
        map.put("content", oyText.getText().toString());
        String json = StringUtil.toJson(map);
        try {
            String data = AESCrypt.getInstance().encrypt(json);
            String d = StringUtil.replaceBlank(data);
            presenter.complaint(d);
        } catch (Exception ex) {
        }
    }

    @Override
    public void success(final Users users) {
        Glide.with(getContext())
                .load(Const.BASE_URL+"Uploads/"+users.avatar)
                .fitCenter()
                .crossFade()
                .transform(new GlideCircleTransform(getContext()))
                .placeholder(R.drawable.adm)
                .into(cz_image);

        cz_name.setText(com.zy.util.StringUtil.isEmpty(users.username)?"未命名":users.username);
        cz_carnumber.setText(com.zy.util.StringUtil.isEmpty(users.mobile)?"未绑定手机号":"电话："+users.mobile);


    }

    @Override
    public void error() {

    }

    @Override
    public void successc() {
        toast("投诉成功");
        finish();
    }

    @Override
    public void errorc() {
        toast("投诉失败，请检查网络");
    }
}
